package cn.hzy.admin.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.UUID;

@Controller
public class MainController extends BaseController {

    private String title = "首页";

    @GetMapping("/")
    public String main(Model model) {
        model.addAttribute("title", title);
        return this.view("main");
    }

    @GetMapping("/home")
    public String home(Model model, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        model.addAttribute("title", title);
        model.addAttribute("time", new Date());
        model.addAttribute("name", "HZY");
        model.addAttribute("age", 20);
        // request
        httpServletRequest.setAttribute("req", "req_" + UUID.randomUUID());
        //session
        httpServletRequest.getSession().setAttribute("sessionid", UUID.randomUUID());
        //app
        httpServletRequest.getSession().getServletContext().setAttribute("app", UUID.randomUUID());
        return this.view("home");
    }

    @GetMapping("/res/{id}/{name}")
    @ResponseBody
    public String restful(@PathVariable String id, @PathVariable String name) {
        return id + "_" + name;
    }

}
